package controllers;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;

import mail.EmailUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@Controller
public class ControllerContacter {
	@RequestMapping(value = "/contact")
	public ModelAndView contact() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("contact");
		return mv;

	}

	@RequestMapping(value = "/contacter", method = RequestMethod.POST)
	public ModelAndView contacter(@RequestParam("nom") String nom,
			@RequestParam("email") String email,
			@RequestParam("sujet") String sujet,
			@RequestParam("message") String message) {

		String CorpMessage = "Message de :" + nom + " dont l'email est " + email
				+ "<br>";
		CorpMessage += message;

		//Ensuite on s'envoit l'email
		try {
			EmailUtil.sendEmailSSL("belfortair@gmail.com", sujet, CorpMessage);
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new ModelAndView(new RedirectView("home"));

	}
}
